/*
 * @author java_deng
 * @date 2024/12/17 9:36
 * @description
 */
package org.kaka.service.impl;

import com.baomidou.mybatisplus.annotation.TableName;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.kaka.dto.BookDto;
import org.kaka.dto.ChapterDto;
import org.kaka.mapper.BookMapper;
import org.kaka.mapper.ChapterMapper;
import org.kaka.po.BookPo;
import org.kaka.po.ChapterPo;
import org.kaka.service.BookService;
import org.kaka.utils.PageResponseData;
import org.kaka.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private ChapterMapper chapterMapper;
    @Transactional
    @Override
    public PageResponseData<BookVo> list(BookPo bookPo) {
        Page<BookVo> result = PageHelper.startPage(bookPo.getPageNo(), bookPo.getPageSize());
        List<BookVo> list = bookMapper.list(bookPo);
        PageResponseData<BookVo> pageResponseData= new PageResponseData<>(list,result.getTotal(),result.getPages());
        return pageResponseData;
    }

    @Override
    public void save(BookDto bookDto) {
        bookDto.setAuditStatus(1);
        bookMapper.insert(bookDto);
    }

    @Override
    public void update(BookDto bookDto) {
        bookDto.setAuditStatus(1);
        bookMapper.updateById(bookDto);
    }

    public BookVo detail(Long id) {
        BookVo detail = bookMapper.detail(id);
        ChapterPo chapterDto = new ChapterPo();
        chapterDto.setBookId(id);
        List<ChapterDto> list = chapterMapper.list(chapterDto);
        detail.setChapters(list);
        detail.setChapterCount(list.size());
        return detail;
    }

    public Object delete(Long id) {
        bookMapper.deleteById(id);
        return "删除成功";
    }

    public PageResponseData<BookVo> managerList(BookPo bookPo) {
        Page<BookVo> result = PageHelper.startPage(bookPo.getPageNo(), bookPo.getPageSize());
        List<BookVo> list = bookMapper.managerList(bookPo);
        PageResponseData<BookVo> pageResponseData= new PageResponseData<>(list,result.getTotal(),result.getPages());
        return pageResponseData;
    }

    public void audit(BookPo bookPo) {
        BookDto bookDto = new BookDto();
        bookDto.setId(bookPo.getId());
        bookDto.setAuditStatus(bookPo.getAuditStatus());
        bookMapper.updateById(bookDto);
    }
}
